unit ModulesForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, 
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.ComCtrls, System.Generics.Collections, ServerModuleManager, EnhancedServerModule;

type
  TModuleInfo = record
    Name: string;
    Status: string;
    BaseURL: string;
    Description: string;
    RequestCount: Integer;
    ErrorCount: Integer;
    Module: TEnhancedServerModule;
  end;

  TfrmModules = class(TForm)
    pnlTop: TPanel;
    btnRefresh: TButton;
    btnClose: TButton;
    lvModules: TListView;
    btnStartModule: TButton;
    btnStopModule: TButton;
    btnConfigure: TButton;
    StatusBar: TStatusBar;
    pnlModuleInfo: TPanel;
    lblModuleInfo: TLabel;
    procedure btnCloseClick(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnStartModuleClick(Sender: TObject);
    procedure btnStopModuleClick(Sender: TObject);
    procedure btnConfigureClick(Sender: TObject);
    procedure lvModulesDblClick(Sender: TObject);
    procedure lvModulesSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
  private
    FModules: TList<TModuleInfo>;
    procedure RefreshModuleList;
    procedure UpdateModuleInfo;
    function GetSelectedModule: TModuleInfo;
    procedure UpdateUI;
  public
    { Public declarations }
  end;

var
  frmModules: TfrmModules;

implementation

{$R *.dfm}

procedure TfrmModules.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmModules.btnRefreshClick(Sender: TObject);
begin
  RefreshModuleList;
end;

procedure TfrmModules.btnStartModuleClick(Sender: TObject);
var
  ModuleInfo: TModuleInfo;
begin
  ModuleInfo := GetSelectedModule;
  if ModuleInfo.Name <> '' then
  begin
    // 在实际实现中，这里应该调用模块的启动方法
    MessageDlg('Would start module: ' + ModuleInfo.Name, mtInformation, [mbOK], 0);
    RefreshModuleList;
  end;
end;

procedure TfrmModules.btnStopModuleClick(Sender: TObject);
var
  ModuleInfo: TModuleInfo;
begin
  ModuleInfo := GetSelectedModule;
  if ModuleInfo.Name <> '' then
  begin
    // 在实际实现中，这里应该调用模块的停止方法
    MessageDlg('Would stop module: ' + ModuleInfo.Name, mtInformation, [mbOK], 0);
    RefreshModuleList;
  end;
end;

procedure TfrmModules.btnConfigureClick(Sender: TObject);
var
  ModuleInfo: TModuleInfo;
begin
  ModuleInfo := GetSelectedModule;
  if ModuleInfo.Name <> '' then
  begin
    // 在实际实现中，这里应该打开模块的配置对话框
    MessageDlg('Would configure module: ' + ModuleInfo.Name, mtInformation, [mbOK], 0);
  end;
end;

procedure TfrmModules.FormCreate(Sender: TObject);
begin
  FModules := TList<TModuleInfo>.Create;
  RefreshModuleList;
end;

procedure TfrmModules.lvModulesDblClick(Sender: TObject);
begin
  btnConfigureClick(Sender);
end;

procedure TfrmModules.lvModulesSelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
begin
  UpdateModuleInfo;
  UpdateUI;
end;

procedure TfrmModules.RefreshModuleList;
var
  ModuleInfo: TModuleInfo;
  I: Integer;
begin
  FModules.Clear;
  
  // 示例数据 - 实际应该从模块管理器获取
  ModuleInfo.Name := 'AuthModule';
  ModuleInfo.Status := 'Running';
  ModuleInfo.BaseURL := '/api/auth';
  ModuleInfo.Description := 'Authentication and authorization services';
  ModuleInfo.RequestCount := 1250;
  ModuleInfo.ErrorCount := 12;
  FModules.Add(ModuleInfo);
  
  ModuleInfo.Name := 'UserModule';
  ModuleInfo.Status := 'Running';
  ModuleInfo.BaseURL := '/api/users';
  ModuleInfo.Description := 'User management and profiles';
  ModuleInfo.RequestCount := 890;
  ModuleInfo.ErrorCount := 5;
  FModules.Add(ModuleInfo);
  
  ModuleInfo.Name := 'ProductModule';
  ModuleInfo.Status := 'Stopped';
  ModuleInfo.BaseURL := '/api/products';
  ModuleInfo.Description := 'Product catalog and inventory';
  ModuleInfo.RequestCount := 0;
  ModuleInfo.ErrorCount := 0;
  FModules.Add(ModuleInfo);
  
  ModuleInfo.Name := 'OrderModule';
  ModuleInfo.Status := 'Running';
  ModuleInfo.BaseURL := '/api/orders';
  ModuleInfo.Description := 'Order processing and management';
  ModuleInfo.RequestCount := 456;
  ModuleInfo.ErrorCount := 8;
  FModules.Add(ModuleInfo);
  
  ModuleInfo.Name := 'ReportModule';
  ModuleInfo.Status := 'Running';
  ModuleInfo.BaseURL := '/api/reports';
  ModuleInfo.Description := 'Reporting and analytics';
  ModuleInfo.RequestCount := 234;
  ModuleInfo.ErrorCount := 3;
  FModules.Add(ModuleInfo);

  // 更新列表视图
  lvModules.Items.BeginUpdate;
  try
    lvModules.Items.Clear;
    
    for I := 0 to FModules.Count - 1 do
    begin
      with lvModules.Items.Add do
      begin
        Caption := FModules[I].Name;
        SubItems.Add(FModules[I].Status);
        SubItems.Add(FModules[I].BaseURL);
        SubItems.Add(FModules[I].Description);
        SubItems.Add(IntToStr(FModules[I].RequestCount));
        SubItems.Add(IntToStr(FModules[I].ErrorCount));
        Data := Pointer(I); // 存储索引以便快速查找
      end;
    end;
    
  finally
    lvModules.Items.EndUpdate;
  end;
  
  // 更新状态栏
  StatusBar.Panels[0].Text := Format('Total modules: %d', [FModules.Count]);
  StatusBar.Panels[1].Text := Format('Running: %d', [FModules.Count - 1]); // 简化计算
  StatusBar.Panels[2].Text := FormatDateTime('hh:nn:ss', Now);
  
  UpdateUI;
end;

procedure TfrmModules.UpdateModuleInfo;
var
  ModuleInfo: TModuleInfo;
begin
  ModuleInfo := GetSelectedModule;
  if ModuleInfo.Name <> '' then
  begin
    lblModuleInfo.Caption := Format('%s - %s | Requests: %d | Errors: %d', 
      [ModuleInfo.Name, ModuleInfo.Description, ModuleInfo.RequestCount, ModuleInfo.ErrorCount]);
  end
  else
  begin
    lblModuleInfo.Caption := 'Select a module to view details...';
  end;
end;

function TfrmModules.GetSelectedModule: TModuleInfo;
begin
  Result := Default(TModuleInfo);
  if (lvModules.Selected <> nil) and (lvModules.Selected.Data <> nil) then
  begin
    Result := FModules[Integer(lvModules.Selected.Data)];
  end;
end;

procedure TfrmModules.UpdateUI;
var
  ModuleInfo: TModuleInfo;
begin
  ModuleInfo := GetSelectedModule;
  
  btnStartModule.Enabled := (ModuleInfo.Name <> '') and (ModuleInfo.Status = 'Stopped');
  btnStopModule.Enabled := (ModuleInfo.Name <> '') and (ModuleInfo.Status = 'Running');
  btnConfigure.Enabled := (ModuleInfo.Name <> '');
end;

initialization
  // 初始化代码（如果需要）

finalization
  // 清理代码（如果需要）

end.